<template>
  <div>
    <el-dialog class="dialog-select-tree dialog-dark-skin" title="选择图标" :visible.sync="myDialogVisible" width="30%" :append-to-body="true">
      <div class="select-tree">
        <el-scrollbar
          tag="div"
          class='is-empty'
          wrap-class="el-select-dropdown__wrap"
          view-class="el-select-dropdown__list">
          <div class="icons-wrapper">
            <div class="icon-item" :class="{active: item === activeIcon}" v-for="item of iconsMap" :key="item" @click="activeIcon = item">
                <svg-icon :icon-class="item" />
            </div>
          </div>
        </el-scrollbar>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="myDialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="selectIcon()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import icons from '@/views/svg-icons/generateIconsView'

  export default {
    props: {
      dialogVisible: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        iconsMap: [],
        activeIcon: '',
        myDialogVisible: false
      }
    },
    watch: {
      dialogVisible(val) {
        this.myDialogVisible = val
      },
      myDialogVisible(val) {
        this.$emit('visibleChange', this.myDialogVisible)
      }
    },
    mounted() {
      const iconsMap = icons.state.iconsMap.map((i) => {
        return i.default.id.split('-')[1]
      })
      this.iconsMap = iconsMap
    },
    methods: {
      selectIcon() {
        this.$emit('selectIcon', this.activeIcon)
        this.myDialogVisible = false
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/variables.scss";
  .dialog-select-tree{
    .icons-wrapper{
      padding: 0 6px;
      .icon-item{
        display: inline-block;
        width: 9.5%;
        text-align: center;
        font-size: 20px;
        line-height: 30px;
        border-radius: 4px;
        margin: 1px;
        cursor: pointer;
        &:hover{
          background-color: darken(#6a8abe, 3%);
          color: #FFF;
        }
        &.active{
          background-color: $menuBg;
          color: #FFF;
        }
      }
    }
  }
</style>
